<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
class plugin_core{
	//扫码传参
	public function qr_scan($qr){
		global $_AR;
		$senddata['touser'] = $qr['openid'];
		$senddata['msgtype'] = 'text';
		$qr['data']=json_decode($qr['data'],true);
		if(!empty($qr['openid'])){
			$openid = $qr['openid'];
		}else{
			if($qr['data']['scantype']=='follow'){
				$text = '认证服务号才能使用带参扫码哦！';
			}elseif($qr['data']['scantype']=='kefu'){
				$text = '认证服务号才能使用微信客服哦！';
			}else{
				$text = '认证服务号才能使用带参扫码哦！';
			}
			$senddata['text']['content'] = $text;
			$res = cfc('account')->sendCustomNotice($senddata);
			return null;
		}
		$fans=pdo_get('core_fans',array('openid'=>$openid,'uniacid'=>$_SESSION['uniacid']));
		if($qr['data']['scantype']=='follow' || $qr['eventtype']=='1'){
			//没有获取过关注奖励
			$credit1=intval($follow['credit1']);
			if(!$fans['credit1time'] && $credit1){
				pdo_update('core_fans',array('credit1 +='=>$credit1),array('openid'=>$openid,'uniacid'=>$_SESSION['uniacid']));
				$scancontent='{nickname}~感谢您的关注！！恭喜您获得[credit1]积分！';
			}else{
				$scancontent='{nickname}~您已关注本公众号！';
				$credit1=0;
			}
			$follow=uni_setting('follow_back');
			$follow['scancontent']=$follow['scancontent']?:$scancontent;
			$search=array(
				'nickname'=>$fans['nickname'],
				'credit1'=>$credit1,
			);
			$content=$follow['scancontent']?cfc('tools')->replace($search,$follow['scancontent']):'';
			$senddata['text']['content'] = $content;
		}elseif($qr['data']['scantype']=='kefu' || $qr['eventtype']=='3'){
			$kefuset=uni_setting('kefuset');
			$kf_account = $kefuset['kf_account'];
			$content = cfc('kefu')->setkfsession($kf_account,$openid);
			if($content['errcode'] != 0){
				$text = cfc('kefu')->error_code($content['errcode']);
				$senddata['text']['content'] = $text;
				$res = cfc('account')->sendCustomNotice($senddata);
				return null;
			}
			$text =  '您好'.$fans['nickname'].'！我是客服。有任何问题，请直接发信息咨询！';
			$senddata['text']['content'] = $text;
			$senddata['customservice']=$kf_account;
		}elseif($qr['data']['scantype']=='back' || $qr['eventtype']=='2'){
			$back=pdo_get('core_back',array('id'=>$qr['backid'],'uniacid'=>$_SESSION['uniacid']));
			//文本消息回复处理
			if(!empty($back)){
				$message=$qr['message'];
				$return=array(
						'ToUserName'=>$message['FromUserName'],
						'FromUserName'=>$message['ToUserName'],
						'CreateTime'=>TIMESTAMP
				);
				$back['content']=json_decode($back['content'],true);
				$medias=array('3','4','5');
				if(in_array($back['backtype'],$medias) && !$back['content']['media_id']){
					if(!$back['content']['url']){
						exi('发送图片/语音/视频消息时，未设置素材地址！','error');
					}
					$types=array('3'=>'1','4'=>'2','5'=>'3');
					$back['content']['media_id']=cfc('account')->add_media($back['content']['url'],$types[$back['backtype']]);
				}
				if($back['backtype']==6){
					$back['content']['musicurl']=tomedia($back['content']['musicurl']);
					$back['content']['thumb_media_id']=cfc('account')->add_media($back['content']['picurl'],1);
				}
				if($back['backtype']==5){
					$back['content']['thumb_media_id']=cfc('account')->add_media($back['content']['picurl'],1);
				}
				if($back['backtype']==8){
							if(!$back['plugin']){
								exi('事件回复缺少模块标识！','error');
							}
							if($back['content']['op']){
								$_RQ['op']=$back['content']['op'];
							}
							$resp=cfc($back['plugin'])->$back['content']['event']($message,$back);
							if($resp){
								$resp['ToUserName']=$resp['ToUserName']?$resp['ToUserName']:$message['FromUserName'];
								$resp['FromUserName']=$resp['FromUserName']?$resp['FromUserName']:$message['ToUserName'];
								$resp['CreateTime']=$resp['CreateTime']?$resp['CreateTime']:TIMESTAMP;
								respond($resp);
							}
							exi('');
				}
				cfc('account',$_AR['wx']['id'])->sendBackNotice($back,$return);
			}
			return null;
		}else{
			return null;
		}
		if($senddata['text']['content']){
			$res = cfc('account')->sendCustomNotice($senddata);
		}
		return null;
	}
}